<?php
/**
 * Created by PhpStorm.
 * User: wangjiangtao
 * Date: 2022/7/14
 * Time: 18:37
 */

namespace App\Modules\Popularize\Platform;


class PlatformKuaishou {

    protected $platform;
    protected $device;
    protected $userId;

    protected $srcType = 'APP';//数据源类型
    protected $userIdType = 'OAID';//用户标识类型

    const ACTIVITE_EVENT = '1';//自定义激活
    const TOMORROW_EVENT = '7';//次日激活
    const REGISTER_EVENT = '2';//自定义注册
    const PAY_EVENT = '3';//自定义付费

    protected $callback='http://ad.partner.gifshow.com/track/activate';//用户行为数据上传
    

    public function __construct($device, $userId)
    {
        $this->device = $device;
        $this->userId = $userId;
    }

    //激活
    public function actviteEvent($tomorrow=0)
    {
        if($tomorrow)
        {
            $code = self::TOMORROW_EVENT;//次日激活
        }else{
            $code = self::ACTIVITE_EVENT;//首次激活
        }
        write_log('Advertisement', '快手回传eventype', $code);
        $requestData = $this->_buildRequestData($code);

        if (empty($requestData)) {
            return false;
        }
        return $this->_report($code, $requestData);
    }

    //注册
    public function registerEvent()
    {
        $requestData = $this->_buildRequestData(self::REGISTER_EVENT);
        if (empty($requestData)) {
            return false;
        }
        return $this->_report(self::REGISTER_EVENT, $requestData);
    }

    //付费
    public function payEvent($amount=0)
    {
        $requestData = $this->_buildRequestData(self::PAY_EVENT, $amount);
        if (empty($requestData)) {
            return false;
        }
        return $this->_report(self::PAY_EVENT, $requestData);
    }

    //组成请求参数
    private function _buildRequestData($type, $payAmount = 0){
        //callback获取
        $parsed_url = parse_url($this->device['request_info']['requestId']);
        parse_str($parsed_url['query'], $params);
        
        if(empty($params)){
            $params['callback'] = '';
        }
        //明细数据列表
        $dataList = [
            'event_type' => $type,
            'event_time' => time() * 1000,
            'callback' => $params['callback'],
        ];
        if($type == self::PAY_EVENT) {
            //单位元保留两位小数
            $dataList['purchase_amount'] = round($payAmount,2);
        }
        if($type == self::TOMORROW_EVENT)
        {
            $dataList['event_props'] = 1;
        }
        write_log('Advertisement', '快手回传参数', $dataList);
        return $dataList;
    }
    private function _report($type, $requestData){
        if (empty($requestData)) {
            return false;
        }
        try{
            write_log('Advertisement', '快手回传参数', $requestData);
            $result = curl_request($this->callback, $requestData, 'urlencoded', 'GET');
            write_log('Advertisement', '快手回传结果', $result);
        }catch (\Exception $exception){
            write_log('Advertisement', '快手回传结果错误', $exception->getMessage());
            return false;
        }
        $result = json_decode($result);

        return isset($result->code) &&  $result->code == 0;
    }

   
}
